package com.zmx.b2c.order.web;

import com.zmx.b2c.order.contents.CreateOrderContents;
import com.zmx.b2c.order.dto.B2COrderDto;
import com.zmx.b2c.order.dto.params.*;
import com.zmx.b2c.order.dto.result.OrderDetailResultDTO;
import com.zmx.b2c.order.dto.result.SumOrderResultDTO;
import com.zmx.b2c.order.service.B2COrderService;
import com.zmx.common.common.invocation.InvocationInfoProxy;
import com.zmx.common.common.web.AbstractController;
import com.zmx.common.common.web.rest.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 订单控制器
 * @ClassName: OrderController
 * @Author zhaomxr
 * @Date 2021-10-22 15:41
 */
@Api(value = "订单控制器")
@Slf4j
@RestController
@RequestMapping("test/order")
public class OrderController extends AbstractController<B2COrderDto, B2COrderService> {

    @Autowired
    private B2COrderService b2COrderService;

    @ApiOperation(value = "构建订单显示信息", tags = "构建订单显示信息")
    @GetMapping(value = "orderShow")
    public Object orderShow(@RequestBody OrderShowParamsDTO params) {
        log.info("begin to orderShowParamsDTO =>" + params);

        try {
            OrderShowDTO orderShowDTO = b2COrderService.orderShow(params);
            return ResponseResult.success(orderShowDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail("构建订单显示信息失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "创建b2c订单", tags = "创建b2c订单")
    @PostMapping(value = "createOrder")
    public ResponseResult createOrder(@RequestBody CreateOrderParams params) {
        log.info("begin to createOrder =>" + params);
        try {
            //校验订单参数
            b2COrderService.checkOrderParams(params);
            //创建订单
            OrderParamsDTO orderParamsDTO = b2COrderService.createOrder(params);
            log.info("订单创建成功！orderSn：{}", orderParamsDTO.getOrderSn());

            return ResponseResult.success(orderParamsDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail("创建订单失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "计算订单总金额", tags = "计算订单总金额")
    @PostMapping(value = "sumOrderAmount")
    public ResponseResult sumOrderAmount(@RequestBody List<ProductParamsDTO> list) {
        log.info("begin to sumOrderAmount orderShowParamsDTO=>" + list);
        try {
            SumOrderResultDTO sumOrderResultDTO = b2COrderService.sumOrderTotalAmount(list);
            return ResponseResult.success(sumOrderResultDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail(CreateOrderContents.RESULT_ERROR_MSG);
        }
    }

    @ApiOperation(value = "用户主动取消订单", tags = "用户主动取消订单")
    @PostMapping(value = "cancelOrder")
    public ResponseResult cancelOrder(@RequestParam(required = true) String orderSn) {
        log.info("begin to cancelOrder orderSn: {}", orderSn);
        try {
            b2COrderService.cancelOrder(orderSn);
            return ResponseResult.success(CreateOrderContents.RESULT_SUCCESS_MSG);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail(CreateOrderContents.RESULT_ERROR_MSG);
        }
    }

    @ApiOperation(value = "确认收货", tags = "确认收货")
    @PostMapping(value = "shippedOrder")
    public ResponseResult shippedOrder(@RequestParam(required = true) String orderSn) {
        log.info("begin to shippedOrder orderSn: {}", orderSn);
        try {
            b2COrderService.shippedOrder(orderSn);
            return ResponseResult.success(CreateOrderContents.RESULT_SUCCESS_MSG);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail(CreateOrderContents.RESULT_ERROR_MSG);
        }
    }

    /**
     * ---------------------------查询显示订单信息--------------------------
     **/

    @ApiOperation(value = "分页获取订单列表", tags = "分页获取订单列表")
    @GetMapping(value = "getOrderPage")
    public ResponseResult getOrderPage(@RequestBody SearchOrderParamsDTO searchOrderParamsDTO) {
        log.info("begin to getOrderPage  searchOrderParamsDTO: {}", searchOrderParamsDTO.toString());
        try {
            Page orderPage = b2COrderService.getOrderPage(searchOrderParamsDTO);
            return ResponseResult.success(orderPage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail("分页获取订单列表失败");
        }
    }

    @ApiOperation(value = "根据订单id查询订单详细信息", tags = "根据订单id查询订单详细信息")
    @GetMapping(value = "getOrderDetailByOrderId")
    public ResponseResult getOrderDetailByOrderId(@RequestParam(required = true) Long orderId) {
        log.info("begin to getOrderDetailByOrderId  orderId: {}", orderId);
        try {
            OrderDetailResultDTO orderDetailResultDTO = b2COrderService.getOrderDetailByOrderId(orderId);
            return ResponseResult.success(orderDetailResultDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail("获取订单详细信息失败");
        }
    }

    @ApiOperation(value = "根据订单orderSn查询订单详细信息", tags = "根据订单orderSn查询订单详细信息")
    @GetMapping(value = "getOrderDetailByOrderSn")
    public ResponseResult getOrderDetailByOrderSn(@RequestParam(required = true) String orderSn) {
        log.info("begin to getOrderDetailByOrderSn  orderSn: {}", orderSn);
        try {
            long startTiem = System.currentTimeMillis();
            System.out.println("startTiem: {}" + startTiem);
            OrderDetailResultDTO orderDetailResultDTO = b2COrderService.getOrderDetailByOrderSn(orderSn);
            System.out.println("总用时: {}" + (System.currentTimeMillis()-startTiem));
            return ResponseResult.success(orderDetailResultDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseResult.fail("获取订单详细信息失败");
        }
    }

    /** ---------------------------查询显示订单信息-------------------------- **/

}
